package receivers;

import com.example.maze.ConnectionInfo;
import com.example.maze.LobbyActivity;
import com.example.maze.RoomActivity;

import tw.edu.tku.im.maze.EnterRoomRep;
import tw.edu.tku.im.maze.RepPacket;
import tw.edu.tku.im.maze.TimeOutRep;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class JoinTheGameRepReceiver extends BroadcastReceiver {
   Activity currentUIActivity;
   
   public JoinTheGameRepReceiver(Activity activity) {
      this.currentUIActivity= activity;
   }

   @Override
   public void onReceive(Context context, Intent intent) {
      ((LobbyActivity)currentUIActivity).dismissMyDialog();
      RepPacket rep= intent.getParcelableExtra("packet");
      if(rep instanceof TimeOutRep) {
         ((LobbyActivity)currentUIActivity).showMsgDialog("連線逾時");
      } else {
         EnterRoomRep mEnterRoomRep= (EnterRoomRep)rep;
         if(mEnterRoomRep.getState() == 0) {
            ConnectionInfo.theUpdateService = null;
            currentUIActivity.startActivity(new Intent(currentUIActivity, RoomActivity.class));
         } else if(mEnterRoomRep.getState() == 1){
            android.util.Log.d("DebugMsg", "加入失敗: 該房間已不存在");
         } else if(mEnterRoomRep.getState() == 2){
            android.util.Log.d("DebugMsg", "加入失敗: 該房間遊戲進行中");
         } else if(mEnterRoomRep.getState() == 3){
            android.util.Log.d("DebugMsg", "加入失敗: 該房間人數已滿");
         } else if(mEnterRoomRep.getState() == 4){
            android.util.Log.d("DebugMsg", "加入失敗: 房間密碼錯誤");
         }
      }
   }

}
